package com.bsoft.gol.hcs.followup.entity;

import ctd.persistence.DAOFactory;
import ctd.persistence.annotation.DAOTransaction;
import ctd.util.AppContextHolder;
import com.bsoft.gol.hcs.enumcase.FollowUpStatusEnum;
import com.bsoft.gol.hcs.followup.bean.request.ExecuteFollowUpDTO;
import com.bsoft.gol.hcs.followup.dao.FusAfterHosRecordDAO;
import com.bsoft.gol.hcs.followup.service.HospitalDischargeFollowUpService;
import org.atag.core.exception.ServiceException;
import com.bsoft.gol.hcs.specialist.entity.FusAfterHosRecord;

import java.util.Objects;

/**
 * 出院后随访领域对象
 *
 * @author neoLin - 2022/05/20 10:08
 **/
public class HospitalDischargeFollowUpEntity {

    private HospitalDischargeFollowUpEntity() {

    }

    /**
     * 当前随访对象
     */
    private FusAfterHosRecord current;

    /**
     * 需要更新的随访内容
     */
    private FusAfterHosRecord future;

    /**
     * 随访记录主键
     */
    private String recordId;

    /**
     * 锁定标志 false 未锁定 true 已锁定
     */
    private Boolean lockFlag;

    /**
     * 随访状态 0未随访 1 已随访 2 随访中 9 已终止
     */
    private Byte followUpStatus;


    //锁定 取消锁定 鉴权    随访 取消随访 撤销  暂挂
    private HospitalDischargeFollowUpService service;

    /**
     * 锁定
     */
    @DAOTransaction
    public void lock() {
        verify(FollowUpStatusEnum.READY, FollowUpStatusEnum.UN_CONFIRM);
        //尝试加锁
        service.lockRecord(this);
    }

    /**
     * 取消锁定
     */
    public void unlock() {
        verify(FollowUpStatusEnum.READY, FollowUpStatusEnum.UN_CONFIRM);
        service.unlockRecord(this);
    }

    /**
     * 完成随访
     */
    public void finish(ExecuteFollowUpDTO dto) {
        verify(FollowUpStatusEnum.READY, FollowUpStatusEnum.UN_CONFIRM);
        service.finishFollowUp(this, dto);
    }

    /**
     * 暂挂随访 暂挂信息
     */
    public void hangUp(ExecuteFollowUpDTO dto) {
        verify(FollowUpStatusEnum.READY, FollowUpStatusEnum.UN_CONFIRM);
        service.hangUpFollowUp(this, dto);
    }

    /**
     * 取消随访
     */
    public void cancel() {
        verify(FollowUpStatusEnum.READY, FollowUpStatusEnum.UN_CONFIRM);
        service.cancelFollowUp(this);
    }

    /**
     * 恢复随访
     */
    public void recover() {
        verify(FollowUpStatusEnum.CANCEL, FollowUpStatusEnum.UN_CONFIRM);
        service.recoverFollowUp(this);
    }


    /**
     * 校验状态
     */
    private void verify(FollowUpStatusEnum... allowStatus) {
        Byte followUpStatus = this.current.getFollowUpStatus();
        boolean valid = false;
        for (FollowUpStatusEnum status : allowStatus) {
            if(status.getStatus() == followUpStatus){
                valid = true;
                break;
            }
        }
        if(!valid){
            throw new ServiceException("当前随访状态不正确,请重新操作");
        }
    }

    public FusAfterHosRecord getCurrent() {
        return current;
    }


    public FusAfterHosRecord getFuture() {
        return future;
    }

    public void setFuture(FusAfterHosRecord future) {
        this.future = future;
    }

    public Boolean getLockFlag() {
        return lockFlag;
    }


    public Byte getFollowUpStatus() {
        return followUpStatus;
    }

    public String getRecordId() {
        return recordId;
    }


    public static Builder builder() {
        return new Builder();
    }

    public static final class Builder {
        private FusAfterHosRecord future;
        private String recordId;

        public Builder() {
        }


        public Builder future(FusAfterHosRecord future) {
            this.future = future;
            return this;
        }

        public Builder recordId(String recordId) {
            this.recordId = recordId;
            return this;
        }

        public HospitalDischargeFollowUpEntity build() {
            HospitalDischargeFollowUpEntity entity = new HospitalDischargeFollowUpEntity();
            FusAfterHosRecordDAO dao = DAOFactory.getDAO(FusAfterHosRecordDAO.class);
            FusAfterHosRecord current = dao.get(recordId);
            if (Objects.isNull(current)) {
                throw new ServiceException("随访记录不存在");
            }
            entity.future = future;
            entity.recordId = recordId;
            entity.service = AppContextHolder.get().getBean(HospitalDischargeFollowUpService.class);
            entity.current = current;
            entity.followUpStatus = current.getFollowUpStatus();
            entity.lockFlag = current.getLockFlag();
            return entity;
        }
    }
}
